﻿function notFill(obj, msg) {
    if ($(obj).val() == "") {
        alert(msg);
        $(obj).focus();
        return true;
    }
    return false;
}

function setVisibleField(objname, Action) {
    //***** Control หลัก
    var ctl = document.getElementById(objname);
    if (ctl == null) {
        ctl = document.getElementById(objname + '_Uc');
    }
    if (ctl != null)
        ctl.style.visibility = Action;
}

function openDialogsetsize(strform, strparam, hight, width) {
    return window.showModalDialog(strform + '?' + strparam, self, "dialogHeight:" + String(hight) + "px;dialogWidth:" + String(width) + "px;center:yes;scroll:yes");
}

function addCommas(strValue) {
    var objRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
    while (objRegExp.test(strValue)) {
        strValue = strValue.replace(objRegExp, '$1,$2');
    }
    return strValue;
}

function ffloat(obj, args, n1, n2, w1, w2) {
    //if (args == "") return "";
    var exists_isth = (typeof (isth) != 'undefined')
    var myisth = true;
    if (exists_isth) myisth = isth;
    var oldvalue = args;
    var purenum = removeCommas('' + twoPointN(removeCommas('' + args)));
    var a = purenum.split('.');

    //ตั้งค่าสูงสุด
    var nr = '';
    for (i = 0; i < (n1 - n2); i++) {
        nr = nr + '9';
    }
    nr = nr + '.99';
    if (w2 == 'N')
        w2 = parseFloat(nr);
    //ตรวจสอบค่าไม่ต่ำกว่า
    if (w1 == 'N')
        w1 = parseFloat('-' + nr);

    //เตือนไม่ให้เกินช่วงค่าเกินที่ระบุ
    if (parseFloat(purenum) < w1) {
        if (myisth)
            alert('ค่าที่ระบุต้องไม่ต่ำกว่า ' + addCommas('' + w1));
        else
            alert('The specified value must not be less than ' + addCommas('' + w1) + '.');
        obj.focus();
        SetEnd(obj);
        return false;
    }
    //ตรวจสอบค่าไม่มากกว่า
    if (parseFloat(purenum) > w2) {
        if (myisth)
            alert('ค่าที่ระบุต้องไม่เกิน ' + addCommas('' + w2));
        else
            alert('The specified value must not exceed ' + addCommas('' + w2) + '.');
        obj.focus();
        SetEnd(obj);
        return false;
    }
    obj.value = twoPointN('' + purenum);
    return true;
}

function iint(obj, args, n, w1, w2) {
    //if (args == "") return "";
    var exists_isth = (typeof (isth) != 'undefined')
    var myisth = true;
    if (exists_isth) myisth = isth;
    var oldvalue = args;
    var purenum = removeCommas('' + twoPointN(removeCommas('' + args)));
    var a = purenum.split('.');

    //ตั้งค่าสูงสุด
    var nr = '';
    for (i = 0; i < n; i++) {
        nr = nr + '9';
    }
    nr = nr + '';
    if (w2 == 'N')
        w2 = parseFloat(nr);
    //ตรวจสอบค่าไม่ต่ำกว่า
    if (w1 == 'N')
        w1 = parseFloat('-' + nr);

    //เตือนไม่ให้เกินช่วงค่าเกินที่ระบุ
    if (parseFloat(purenum) < w1) {
        if (myisth)
            alert('ค่าที่ระบุต้องไม่ต่ำกว่า ' + w1);
        else
            alert('The specified value must not be less than ' + w1 + '.');
        obj.focus();
        SetEnd(obj);
        return false;
    }
    //ตรวจสอบค่าไม่มากกว่า
    if (parseFloat(purenum) > w2) {
        if (myisth)
            alert('ค่าที่ระบุต้องไม่เกิน ' + w2);
        else
            alert('The specified value must not exceed ' + w2 + '.');
        obj.focus();
        SetEnd(obj);
        return false;
    }
    obj.value = addCommas('' + a[0]);
    return true;
}

function iintNoCommas(obj, args, n, w1, w2) {
    //if (args == "") return "";
    var exists_isth = (typeof (isth) != 'undefined')
    var myisth = true;
    if (exists_isth) myisth = isth;
    var oldvalue = args;
    var purenum = removeCommas('' + twoPointN(removeCommas('' + args)));
    var a = purenum.split('.');

    //ตั้งค่าสูงสุด
    var nr = '';
    for (i = 0; i < n; i++) {
        nr = nr + '9';
    }
    nr = nr + '';
    if (w2 == 'N')
        w2 = parseFloat(nr);
    //ตรวจสอบค่าไม่ต่ำกว่า
    if (w1 == 'N')
        w1 = parseFloat('-' + nr);

    //เตือนไม่ให้เกินช่วงค่าเกินที่ระบุ
    if (parseFloat(purenum) < w1) {
        if (myisth)
            alert('ค่าที่ระบุต้องไม่ต่ำกว่า ' + w1);
        else
            alert('The specified value must not be less than ' + w1 + '.');
        obj.focus();
        SetEnd(obj);
        return false;
    }
    //ตรวจสอบค่าไม่มากกว่า
    if (parseFloat(purenum) > w2) {
        if (myisth)
            alert('ค่าที่ระบุต้องไม่เกิน ' + w2);
        else
            alert('The specified value must not exceed ' + w2 + '.');
        obj.focus();
        SetEnd(obj);
        return false;
    }
    obj.value = a[0];
    return true;
}

function iintNoCommasNoZero(obj, args, n, w1, w2) {
    //if (args == "") return "";
    var exists_isth = (typeof (isth) != 'undefined')
    var myisth = true;
    if (exists_isth) myisth = isth;
    var oldvalue = args;
    var purenum = removeCommas('' + twoPointN(removeCommas('' + args)));
    var a = purenum.split('.');

    //ตั้งค่าสูงสุด
    var nr = '';
    for (i = 0; i < n; i++) {
        nr = nr + '9';
    }
    nr = nr + '';
    if (w2 == 'N')
        w2 = parseFloat(nr);
    //ตรวจสอบค่าไม่ต่ำกว่า
    if (w1 == 'N')
        w1 = parseFloat('-' + nr);

    //เตือนไม่ให้เกินช่วงค่าเกินที่ระบุ
    if (parseFloat(purenum) < w1) {
        if (myisth)
            alert('ค่าที่ระบุต้องไม่ต่ำกว่า ' + w1);
        else
            alert('The specified value must not be less than ' + w1 + '.');
        obj.focus();
        SetEnd(obj);
        return false;
    }
    //ตรวจสอบค่าไม่มากกว่า
    if (parseFloat(purenum) > w2) {
        if (myisth)
            alert('ค่าที่ระบุต้องไม่เกิน ' + w2);
        else
            alert('The specified value must not exceed ' + w2 + '.');
        obj.focus();
        SetEnd(obj);
        return false;
    }
    if (a[0] == '0')
        obj.value = '';
    else
        obj.value = a[0];
}

function iintNoCommasNoZero(obj, args, n, w1, w2) {
    //if (args == "") return "";
    var exists_isth = (typeof (isth) != 'undefined')
    var myisth = true;
    if (exists_isth) myisth = isth;
    var oldvalue = args;
    var purenum = removeCommas('' + twoPointN(removeCommas('' + args)));
    var a = purenum.split('.');

    //ตั้งค่าสูงสุด
    var nr = '';
    for (i = 0; i < n; i++) {
        nr = nr + '9';
    }
    nr = nr + '';
    if (w2 == 'N')
        w2 = parseFloat(nr);
    //ตรวจสอบค่าไม่ต่ำกว่า
    if (w1 == 'N')
        w1 = parseFloat('-' + nr);

    //เตือนไม่ให้เกินช่วงค่าเกินที่ระบุ
    if (parseFloat(purenum) < w1) {
        if (myisth)
            alert('ค่าที่ระบุต้องไม่ต่ำกว่า ' + w1);
        else
            alert('The specified value must not be less than ' + w1 + '.');
        obj.focus();
        return;
    }
    //ตรวจสอบค่าไม่มากกว่า
    if (parseFloat(purenum) > w2) {
        if (myisth)
            alert('ค่าที่ระบุต้องไม่เกิน ' + w2);
        else
            alert('The specified value must not exceed ' + w2 + '.');
        obj.focus();
        return;
    }
    if (a[0] == '0')
        obj.value = '';
    else
        obj.value = a[0];
}

function ClearIfZero(obj) {
    if (obj.value == '')
        return;
    var finaltotal = parseFloat(removeCommas(obj.value));
    if ((finaltotal == "NaN") || (finaltotal == 0))
        obj.value = '';
    else {
        obj.value = finaltotal;
        SetEnd(obj)
    }
}

function removeCommas(strValue) {
    var objRegExp = /,/g; //search for commas globally
    //replace all matches with empty strings
    return strValue.replace(objRegExp, '');
}

function ClearIfZero(obj) {
    if (obj.value == '')
        return;
    var finaltotal = parseFloat(removeCommas(obj.value));
    if ((finaltotal == "NaN") || (finaltotal == 0))
        obj.value = '';
    else {
        obj.value = finaltotal;
        SetEnd(obj)
    }
}

function onKeyPressAllDigitAndDot(obj, decimal, minusEnable) {
    var ns4 = (document.layers) ? true : false;
    var ie4 = (document.all) ? true : false;
    var key = ie4 ? event.keyCode : e.which;

    if (minusEnable != null && minusEnable) {
        if (!isBizCurrencyByCode(key)) {
            if (ie4) {
                event.returnValue = false;
            }
            else {
                event.returnValue = false;
            }
        }
    }
    else {
        if (!isCurrencyByCode(key)) {
            if (ie4) {
                event.returnValue = false;
            }
            else {
                event.returnValue = false;
            }
        }
    }

    if (key == 46 && !isCurrency(obj)) {
        if (ie4) {
            event.returnValue = false;
        }
        else {
            event.returnValue = false;
        }
    }

    if (minusEnable != null && minusEnable) {
        if (key == 45 && !isBizCurrency(obj)) {
            if (ie4) {
                event.returnValue = false;
            }
            else {
                event.returnValue = false;
            }
        }
    }
}

function iint(obj, args, n, w1, w2) {
    //if (args == "") return "";
    var exists_isth = (typeof (isth) != 'undefined')
    var myisth = true;
    if (exists_isth) myisth = isth;
    var oldvalue = args;
    var purenum = removeCommas('' + twoPointN(removeCommas('' + args)));
    var a = purenum.split('.');

    //ตั้งค่าสูงสุด
    var nr = '';
    for (i = 0; i < n; i++) {
        nr = nr + '9';
    }
    nr = nr + '';
    if (w2 == 'N')
        w2 = parseFloat(nr);
    //ตรวจสอบค่าไม่ต่ำกว่า
    if (w1 == 'N')
        w1 = parseFloat('-' + nr);

    //เตือนไม่ให้เกินช่วงค่าเกินที่ระบุ
    if (parseFloat(purenum) < w1) {
        if (myisth)
            alert('ค่าที่ระบุต้องไม่ต่ำกว่า ' + w1);
        else
            alert('The specified value must not be less than ' + w1 + '.');
        obj.focus();
        SetEnd(obj);
        return false;
    }
    //ตรวจสอบค่าไม่มากกว่า
    if (parseFloat(purenum) > w2) {
        if (myisth)
            alert('ค่าที่ระบุต้องไม่เกิน ' + w2);
        else
            alert('The specified value must not exceed ' + w2 + '.');
        obj.focus();
        SetEnd(obj);
        return false;
    }
    obj.value = addCommas('' + a[0]);
    return true;
}

function twoPointN(value) {
    var finaltotal = parseFloat(removeCommas(value)).toFixed(2);
    if (finaltotal == "NaN")
        finaltotal = "0.00";
    return addCommas('' + finaltotal);
}

function isNumbers(e) {
    var keynum;
    var keychar;
    var numcheck;

    if (window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }
    keychar = String.fromCharCode(keynum);
    if (keychar == '.') return true;
    numcheck = /\d/;
    return numcheck.test(keychar);
}

function SetEnd(TB) {
    if (TB.createTextRange) {
        var FieldRange = TB.createTextRange();
        FieldRange.moveStart('character', TB.value.length);
        FieldRange.collapse();
        FieldRange.select();
    }
}

function GetCurrentPageName() {
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    return sPage.toLowerCase()
}

var dtCh = "/";
var minYear = 1900;
var maxYear = 2100;

if (typeof isth != 'undefined') {
    if (isth) {
        minYear += 543;
        maxYear += 543;
    }
}
function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary(year) {
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    if (isth)
        year -= 543;
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
        if (i == 2) { this[i] = 29 }
    }
    return this
}
function isTime(dtStr) {
    var t = dtStr.split(':');
    var hour = parseInt(t[0]);
    var minute = parseInt(t[1]);
    var second = parseInt(t[2]);

    if (isNaN(hour)) hour = 0;
    if (isNaN(minute)) minute = 0;
    if (isNaN(second)) second = 0;

    if ((hour > 23) || (hour < 0)) {
        if (isth)
            alert("ชั่วโมงต้องมีค่าระหว่าง 0 ถึง 23");
        else
            alert("Hour must between 0 to 23");
        return false;
    }
    else if ((minute > 59) || (minute < 0)) {
        if (isth)
            alert("นาทีต้องมีค่าระหว่าง 0 ถึง 59");
        else
            alert("Minute must between 0 to 59");
        return false;
    }
    if (t.length == 3) {
        if ((second > 59) || (second < 0)) {
            if (isth)
                alert("วินาทีต้องมีค่าระหว่าง 0 ถึง 59");
            else
                alert("Seconds must between 0 to 59");
            return false;
        }
    }
    return true;
}

function isDate(dtStr) {
    if (dtStr == "")
        return true;
    var daysInMonth = DaysArray(12)
    var pos1 = dtStr.indexOf(dtCh)
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
    var strDay = dtStr.substring(0, pos1)
    var strMonth = dtStr.substring(pos1 + 1, pos2)
    var strYear = dtStr.substring(pos2 + 1)
    strYr = strYear
    if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
    if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
    }
    month = parseInt(strMonth)
    day = parseInt(strDay)
    year = parseInt(strYr)
    if (pos1 == -1 || pos2 == -1) {
        if (isth)
            alert("วันที่จะต้องมีรูปแบบเป็น : dd/mm/yyyy")
        else
            alert("The date format should be : dd/mm/yyyy")
        return false
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        if (isth)
            alert("กรุณาใส่เดือนให้ถูกต้อง")
        else
            alert("Please enter a valid month")
        return false
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        if (isth)
            alert("กรุณาใส่วันที่ให้ถูกต้อง")
        else
            alert("Please enter a valid day")
        return false
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
        if (isth)
            alert("กรุณาใส่ปีในช่วง " + minYear + " ถึง " + maxYear)
        else
            alert("Please enter a valid 4 digit year between " + minYear + " and " + maxYear)
        return false
    }
    if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
        if (isth)
            alert("กรูณาใส่วันที่ให้ถูกต้อง")
        else
            alert("Please enter a valid date")
        return false
    }
    return true
}


function settime() {
    //var curtime = new Date();
    //var curhour = <%= DateTime.Now.Hour.ToString() %>;//curtime.getHours();
    //var curmin = <%= DateTime.Now.Minute.ToString() %>;//curtime.getMinutes();
    //var cursec = <%= DateTime.Now.Second.ToString() %>;//curtime.getSeconds();
    if (cursec < 59) {
        cursec++;
    } else if (curmin < 59) {
        curmin++;
        cursec = 0;
    } else if (curhour < 23) {
        curhour++;
        curmin = 0;
        cursec = 0;
    } else {
        curhour = 0;
        curmin = 0;
        cursec = 0;
    }

    var time = "";

    //            if (curhour == 0) curhour = 12;
    //            time = (curhour > 12 ? curhour - 12 : curhour) + ":" +
    //         (curmin < 10 ? "0" : "") + curmin + ":" +
    //         (cursec < 10 ? "0" : "") + cursec + " " +
    //         (curhour > 12 ? "PM" : "AM");

    time = curhour + ":" +
                 (curmin < 10 ? "0" : "") + curmin + ":" +
                 (cursec < 10 ? "0" : "") + cursec;

    $(document).ready(function () {
        $('#currtime').html(time);
    });
}

function setPos(el, objdocument) {
    if (objdocument.getBoxObjectFor) {
        el.style.left = el.x;
        el.style.top = el.y;
    }
    else {
        el.style.pixelLeft = el.x + objdocument.documentElement.scrollTop;
        el.style.pixelTop = el.y + objdocument.documentElement.scrollLeft;
    }
}

function ShowLoad() {
    objdocument = document;
    objwindow = window;
    while (top.document != objdocument) {
        objdocument = top.document
        objwindow = top.window;
    }

    try {
        bgObj = objdocument.getElementById('dialog-overlay');
        if (typeof bgObj != 'undefined') {
            bgObj.style.display = "";
            bgObj.style.width = $(objwindow).width();
            bgObj.style.height = $(objwindow).height();
        }
    } catch (e) { }

    objmsg = objdocument.getElementById('loadingmsg');
    objmsg.x = $(objwindow).width() - $('#loadingmsg').width() + $(objdocument).scrollLeft();
    objmsg.y = $(objwindow).height() - $('#loadingmsg').height() + $(objdocument).scrollTop();
    setPos(objmsg, objdocument);
    $(objmsg).show();
}
function HideLoad() {
    objdocument = document;
    objwindow = window;
    objmsg = objdocument.getElementById('loadingmsg');
    $(objmsg).hide();
    while (top.document != objdocument) {
        objdocument = top.document
        objwindow = top.window;
        objmsg = objdocument.getElementById('loadingmsg');
        $(objmsg).hide();
    }
    objmsg = objdocument.getElementById('loadingmsg');
    $(objmsg).hide();

    try {
        bgObj = objdocument.getElementById('dialog-overlay');
        bgObj.style.display = "none";
    } catch (e) { }
}

function saveSuccess(msg) {
    $('#success_msg').html(msg);

    $.blockUI({
        message: $('div.growlUI'),
        fadeIn: 700,
        fadeOut: 700,
        timeout: 2000,
        showOverlay: false,
        centerY: false,
        css: {
            width: '200px',
            top: '10px',
            left: '',
            right: '10px',
            border: 'none',
            padding: '5px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: .6,
            color: '#fff'
        }
    });
}



function changeFontSizeAction(content, inc) {
    if (content.style.fontSize) {
        var size = parseInt(content.style.fontSize.replace("px", ""));
    } else {
        var size = 14;
    }
    var newSize = size + inc;
    newSize = getMinMaxSize(newSize);
    content.style.fontSize = newSize + 'px';
}

function getMinMaxSize(newSize) {
    var maxSize = 36;
    var minSize = 14;

    if (newSize > maxSize)
        newSize = maxSize;
    else if (newSize < minSize)
        newSize = minSize;

    return newSize;
}

function getSpaceHTML(level) {
    for (i = 0; i < level; i++) {
        document.write('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
    }
}

function getSelection(obj) {
    var textComponent = obj;
    var selectedText;
    // IE version
    if (document.selection != undefined) {
        textComponent.focus();
        var sel = document.selection.createRange();
        selectedText = sel.text;
    }
    // Mozilla version
    else if (textComponent.selectionStart != undefined) {
        var startPos = textComponent.selectionStart;
        var endPos = textComponent.selectionEnd;
        selectedText = textComponent.value.substring(startPos, endPos)
    }
    return selectedText;
}


function maxLength(field, maxChars) {
    if (field.val().length - getSelection(field).length >= parseInt(maxChars)) {
        event.returnValue = false;
        return false;
    }
}

function maxLengthPaste(field, maxChars) {
    event.returnValue = false;
    if ((field.val().length - getSelection(field).length + window.clipboardData.getData("Text").length) > parseInt(maxChars)) {
        return false;
    }
    event.returnValue = true;
}

function ColumnCount(table) {
    return table.find('tr')[0].cells.length;
}

var beforeHeightChange = 0;
var lastHeightChange = 0;
function notSuitableWidth(table) {
    for (i = 0; i < table.find('tr').length; i++) {
        if (table.find('tr')[i].offsetHeight > 23) {
            beforeHeightChange = lastHeightChange;
            lastHeightChange = table.find('tr')[i].offsetHeight;
            return true;
        }
    }
    return false;
}

function autoAdjustWidth(table) {
    var countloop = 0;
    while (notSuitableWidth(table) && (countloop < 25)) {
        table.width(table.width() + 200);
        countloop++;
    }
    if (countloop >= 1)
        table.width(table.width() + 100);
}
